//  ------------ 测试产生虚拟数据 -------------------


//  ------------ 赋值 ------------------------
new Vue({
    el: '#app',
    data: function () {
        return {
            activeName: 'first',
            resourceTypeForm: {
                handle: '',
                resourceType: '',
                description: ''
            },
            selectTypeData: [],
            selectedResData: [],
            selResourceData: [],
            value: '',
            rules: {
                resourceType: [
                    {required: true, message: '请输入资源类型名称', trigger: 'blur'}
                ]
            },
            resourceTypeDialog: false
        }
    },
    methods: {
        queryResouceType(){
            var me = this;
            var hiddenHandleV = $("#resourceTypeHandleHidden").val();
            if (hiddenHandleV === "") {
                me.$notify.info({
                    title: '消息',
                    message: '请先选择资源类型！'
                });
            } else {
                var param = {
                    serviceName: "resourceTypeService",
                    serviceMethodName: "searchOneResourceTypeByHnadle",
                    handle: hiddenHandleV
                };

                function callBack(data) {
                    if (data.resultCode === "S") {
                        me.resourceTypeForm = data.result.ResTypeList[0];
                        me.selResourceData = data.result.UnAssResList;
                        //me.selectedResData = data.result.AssResList;
                        for (var z = 0; z < data.result.AssResList.length; z++) {
                            me.selectedResData.push(data.result.AssResList[z].key);
                        }
                        ;
                    } else {
                        me.$notify.error({
                            title: '检索出错！',
                            message: data.resultMsg
                        });
                    }
                };
                me.ajaxGetRequest(param, callBack);
            }
        },
        saveResourceType(formName){
            var me = this, _form = me.resourceTypeForm;
            me.$refs[formName].validate((valid) => {
                if(valid){
                    var resTypeV = _form.resourceType;
                    var description = _form.description;
                    var _resourceAssList = me.selectedResData;
                    var param = {
                        serviceName: "resourceTypeService",
                        serviceMethodName: "addOrUpdateResourceType",
                        resourceType: resTypeV,
                        description: description,
                        resourceAssList: JSON.stringify(_resourceAssList),
                        plant: "TEST"
                    };
                    function callBack(data) {
                        if (data.resultCode === "S") {
                            me.$notify({
                                title: '成功',
                                message: '保存成功！',
                                type: 'success'
                            });
                        } else {
                            me.$notify.error({
                                title: '保存出错！',
                                message: data.resultMsg
                            });
                        }
                    };
                    me.ajaxGetRequest(param, callBack);
                }else{
                    console.log('error submit!!');
            return false;
        }
        });
        },
        clearResourceType(){
            this.resourceTypeForm = {};
            this.selectedResData = [];
            this.selResourceData = [];
            $("#resourceTypeHandleHidden").val();
        },
        deleteResourceType(){
            var me = this;
            var typHandleV = $("#resourceTypeHandleHidden").val();
            if (typHandleV === "") {
                me.$notify.info({
                    title: '消息',
                    message: '请先执行查询'
                });
            } else {
                var param = {
                    serviceName: "resourceTypeService",
                    serviceMethodName: "deleteResourceType",
                    handle: typHandleV
                };

                function callBack(data) {
                    if (data.resultCode === "S") {
                        me.clearResourceType();
                        me.$notify({
                            title: '成功',
                            message: '删除成功!',
                            type: 'success'
                        });
                    } else {
                        me.$notify.error({
                            title: '删除出错！',
                            message: data.resultMsg
                        });
                    }
                };
                me.ajaxGetRequest(param, callBack);
            }
        },
        resourceTypeSearch(){
            var me = this, _form = me.resourceTypeForm;
            var resTypeV = _form.resourceType;
            var param = {
                serviceName: "resourceTypeService",
                serviceMethodName: "searchDataByResourceType",
                resourceType: resTypeV
            };

            function callBack(data) {
                if (data.resultCode === "S") {
                    me.selectTypeData = data.result;
                    me.resourceTypeDialog = true;
                } else {
                    me.$notify.error({
                        title: '查询出错！',
                        message: data.resultMsg
                    });
                }
            };
            me.ajaxGetRequest(param, callBack);
        },
        selectedRowType(row, evt){
            var handle = row.handle;
            var resourceType = row.resourceType;
            this.resourceTypeForm.resourceType = resourceType;
            $("#resourceTypeHandleHidden").val(handle);
            this.resourceTypeDialog = false;
        },
        ajaxPostRequest(params, callBackFun)
        {
            var me = this, _url = "http://localhost:8083/route";
            axios({
                method: 'post',
                url: _url,
                params: params
            }).then(function (response) {
                if (response.status == 200) {
                    callBackFun(response.data);
                } else {
                    console.log(response);
                }
            }).catch(function (err) {
                console.log(err);
            });
        },
        ajaxGetRequest(params, callBackFun){
            var me = this, _url = "http://localhost:8083/route";
            axios.get(_url, {
                params: params
            }).then(function (response) {
                if (response.status == 200) {
                    callBackFun(response.data);
                } else {
                    console.log(response);
                }
            }).catch(function (err) {
                console.log(err);
            });
        }
    }
})